$(function () {
    // 获取买家产品库
    var loading = weui.loading('loading', {
        className: 'custom-classname'
    });
    var result = Model.user.buyerProxyGoods();
    if (!sessionStorage.getItem('hmdj_stopError')) {
        sessionStorage.setItem('hmdj_stopError', true);
    } else {
        if (result.code) {
            Common.fn.formatOutput(result);
            loading.hide();
            return false;
        }
    }
    for (var i = 0; i < result.data.length; i++) {
        var item = '<div class="pruduct-manager"><div class="weui-cell"><img class="pruduct-img" src="' + result.data[i].photo + '"><div class="weui-cells"><div class="weui-cell"><h3 class="text-1-6">' + result.data[i].content + '</h3></div><div class="weui-cell"><p class="patron-price">￥' + result.data[i].topPrice + '起</p><span class="text-ffb123">' + result.data[i].charges + '%佣金</span></div><div class="weui-cell"><div class="weui-cell__bd "><a href="javascript:;" class="weui-btn weui-btn_mini weui-btn_default cancel-daili" data-id="' + result.data[i].goodsId + '">取消代理</a>';
        // 商品为下架状态，显示重新上架按钮
        if(parseInt(result.data[i].state) == 1) {
            item += '<a href="javascript:;" class="weui-btn weui-btn_mini weui-btn_default resend-btn" data-id="' + result.data[i].proxyId + '">重新上架</a>';
        }
        item += '</div></div></div></div></div>';
        $('.product-lists').append(item);
    }
    loading.hide();
});

// 取消代理
$(document).on('click', '.cancel-daili', function () {
    var id = $(this).attr('data-id');
    weui.confirm('确定要取消代理吗？', {
        title: '系统提示',
        buttons: [{
            label: '取消',
            type: 'default',
            onClick: function () {
                console.log('no')
            }
        }, {
            label: '取消代理',
            type: 'primary',
            onClick: function () {
                var result = Model.user.cancelProxy({
                    goodsId: id
                });
                if (result.code) {
                    alert(result.message);
                    location.reload();
                    return false;
                }
                alert(result.data);
                location.reload();
            }
        }]
    });
});

// 重新上架按钮
$(document).on('click', '.resend-btn', function() {
    var id = $(this).attr('data-id');
    weui.confirm('确定要重新上架吗？', {
        title: '系统提示',
        buttons: [{
            label: '取消',
            type: 'default',
            onClick: function () {
                console.log('no')
            }
        }, {
            label: '重新上架',
            type: 'primary',
            onClick: function () {
                var result = Model.goods.reUpShelve({
                    proxyId: id
                });
                if (result.code) {
                    alert(result.message);
                    location.reload();
                    return false;
                }
                alert(result.data);
                location.reload();
            }
        }]
    });
});